<?php

declare(strict_types=1);

namespace Zms\AdminElementPlus\FormCreate\FormItem;

/**
 * 级联选择器
 * @link https://element-plus.gitee.io/zh-CN/component/cascader.html
 */
class Cascader extends FormItemAbstract
{
    protected string $_type = 'cascader';

    /**
     * Radio constructor.
     * @param string $field
     * @param string $title
     * @param array $value
     */
    public function __construct(string $field, string $title, array $value = [])
    {
        $this->_title = $title;
        $this->_field = $field;
        $this->_value = $value;
        $this->clearable(false);
    }


    /**
     * 获取值类型
     * @return string
     */
    public function valueType(): string
    {
        return self::VALUE_TYPE_ARRAY;
    }

    public function defaultProps(): array
    {
        return [
            'options' => [],
            'separator' => ' / ',
            'collapse-tags' => false,
            'class' => 'form-create-cascader',
            'props' => [
                'value' => 'value',
                'label' => 'label',
                'multiple' => false,
                'children' => 'children',
                'checkStrictly' => false,
            ]
        ];
    }

    /**
     * 可选项数据源
     * @param array $options
     * @return Cascader
     */
    public function options(array $options): Cascader
    {
        return $this->props('options', $options);
    }


    /**
     * 配置选项
     * @param array $props
     * @return Cascader
     */
    public function setProps(array $props): Cascader
    {
        return $this->props('props', $props);
    }


    /**
     * 选项分隔符
     * @param string $separator
     * @return Cascader
     */
    public function separator(string $separator): Cascader
    {
        return $this->props('separator', $separator);
    }


    /**
     * 是否可多选
     * @param bool $multiple
     * @return Cascader
     */
    public function multiple(bool $multiple): Cascader
    {
        $props = $this->getProps();
        $props['props']['multiple'] = $multiple;
        return $this->props('props', $props['props']);
    }


    /**
     * 指定选项的值为选项对象的某个属性值
     * @param string $key
     * @return Cascader
     */
    public function valueKey(string $key): Cascader
    {
        $props = $this->getProps();
        $props['props']['value'] = $key;
        return $this->props('props', $props['props']);
    }


    /**
     * 指定选项标签为选项对象的某个属性值
     * @param string $key
     * @return Cascader
     */
    public function labelKey(string $key): Cascader
    {
        $props = $this->getProps();
        $props['props']['label'] = $key;
        return $this->props('props', $props['props']);
    }


    /**
     * 指定选项的子选项为选项对象的某个属性值
     * @param string $key
     * @return Cascader
     */
    public function childrenKey(string $key): Cascader
    {
        $props = $this->getProps();
        $props['props']['children'] = $key;
        return $this->props('props', $props['props']);
    }


    /**
     * 是否不允许选择任意一级
     * @param bool $checkStrictly
     * @return Cascader
     */
    public function checkStrictly(bool $checkStrictly): Cascader
    {
        $props = $this->getProps();
        $props['props']['checkStrictly'] = $checkStrictly;
        return $this->props('props', $props['props']);
    }


    /**
     * 是否折叠多选标签
     * @param bool $collapseTags
     * @return Cascader
     */
    public function collapseTags(bool $collapseTags): Cascader
    {
        return $this->props('collapse-tags', $collapseTags);
    }

    /**
     * 表单提示
     * @param string $placeholder
     * @return Cascader
     */
    public function placeholder(string $placeholder): Cascader
    {
        return $this->props('placeholder', $placeholder);
    }
}